/**
 * 
 */
package ai.human

import java.util.Collections

import data.Position
import engine.Pitch

/**
 * Positions requirments.
 * 
 * @author Tomasz Dąbrowski
 *
 */
class PositionReqs {

	/**
	 * In scirmmage zone.
	 */
	int inScirmmageZone = 0

	/**
	 * In wide zone left.
	 */
	int inWideZoneLeft = 0

	/**
	 * In wide zone right.
	 */
	int inWideZoneRight = 0

	/**
	 * Players to set.
	 */
	int playersToSet

	/**
	 * Permitted positions.
	 */
	List<Position> positions

	/**
	 * Checks the scrimmage zone.
	 *
	 *
	 * @author Tomasz Dąbrowski
	 */
	private void checkScrimmageZone() {
		if (playersToSet == 3 - inScirmmageZone) {
			positions.removeAll { Position p -> !Pitch.isScrimmageZone(p) }
		} else if (inScirmmageZone == 3) {
			positions.removeAll { Position p -> Pitch.isScrimmageZone(p) }
		}
	}

	/**
	 * Checks the bottom wide zone. 
	 *
	 *
	 * @author Tomasz Dąbrowski
	 */
	private void checkWideZoneBottom() {
		if (inWideZoneLeft == 2) {
			positions.removeAll { Position p -> Pitch.isBottomWideZone(p) }
		}
	}

	/**
	 * Checks the top wide zone.
	 *
	 *
	 * @author Tomasz Dąbrowski
	 */
	private void checkWideZoneTop() {
		if (inWideZoneRight == 2) {
			positions.removeAll { Position p -> Pitch.isTopWideZone(p) }
		}
	}

	/**
	 * Getter for the "positions" property.
	 *
	 * @return the positions
	 *
	 * @author Tomasz Dąbrowski
	 */
	public List<Position> getPositions() {
		return Collections.unmodifiableList(positions)
	}

	/**
	 * Marks the position as used.
	 *
	 * @param position
	 *
	 * @author Tomasz Dąbrowski
	 */
	public void registerPosition(Position position) {
		playersToSet--
		positions.remove(position)
		if (playersToSet == 0) {
			return
		}
		if (Pitch.isBottomWideZone(position)) {
			inWideZoneLeft++;
			checkWideZoneBottom()
		} else if (Pitch.isTopWideZone(position)) {
			inWideZoneRight++;
			checkWideZoneTop()
		} else if (Pitch.isScrimmageZone(position)) {
			inScirmmageZone++;
			checkScrimmageZone();
		} else {
			checkScrimmageZone();
		}
	}
}
